import time

import pytest
import yaml

from pythoncode.calculator import Calculator





@pytest.fixture(autouse=True)
def print_info():
    print("开始计算")
    #yield 前面相当于setup
    yield
    #yield 关键子后面相当于teardown
    print("结束计算")

@pytest.fixture()
def get_calc():
    calc = Calculator()
    return calc

@pytest.fixture(scope="session",autouse=True)
def finish():
    yield
    print("测试结束")

@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    now = time.strftime("%Y-%m-%d_%H-%M-%S")
    log_name = './logs/' + now + '.log'
    request.config.pluginmanager.get_plugin("logging-plugin").set_log_path(log_name)

def pytest_collection_modifyitems(items):
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


def get_data(level):
    with open("./data/case.yml", encoding="utf-8") as f:
        datas = yaml.safe_load(f)
        add_datas = datas.get("add")
        return (add_datas.get(level).get("datas"),add_datas.get(level).get("ids"))

def get_div_data(level):
    with open("./data/divdata.yml", encoding="utf-8") as f:
        datas = yaml.safe_load(f)
        add_datas = datas.get("div")
    return (add_datas.get(level).get("data"), add_datas.get(level).get("ids"))

@pytest.fixture(params=get_data("P0")[0],ids = get_data("P0")[1])
def get_addP0_data(request):
    return request.param

@pytest.fixture(params=get_data("P1_1")[0],ids = get_data("P1_1")[1])
def get_addP1_1_data(request):
    return request.param

@pytest.fixture(params=get_data("P1_2")[0],ids = get_data("P1_2")[1])
def get_addP1_2_data(request):
    return request.param

@pytest.fixture(params=get_data("P2")[0],ids = get_data("P2")[1])
def get_addP2_data(request):
    return request.param

@pytest.fixture(params=get_div_data("P0_1")[0],ids = get_div_data("P0_1")[1])
def get_divP0_1_data(request):
    return request.param

@pytest.fixture(params=get_div_data("P0_2")[0],ids = get_div_data("P0_2")[1])
def get_divP0_2_data(request):
    return request.param

@pytest.fixture(params=get_div_data("P1")[0],ids = get_div_data("P1")[1])
def get_divP1_data(request):
    return request.param

@pytest.fixture(params=get_div_data("P2")[0],ids = get_div_data("P2")[1])
def get_divP2_data(request):
    return request.param